package jscover.instrument.handler;

import java.util.ArrayList;
import java.util.List;

public class SourceHandlerRegister {

    private static final List<SourcePreHandler> preHandlers = new ArrayList<>();
    private static final List<SourcePostHandler> postHandlers = new ArrayList<>();


    public static void registSourcePreHandler(SourcePreHandler handler){
        preHandlers.add(handler);
    }

    public static void registSourcePostHandler(SourcePostHandler handler){
        postHandlers.add(handler);
    }

    public static List<SourcePreHandler> getPreHandlers(){
        return preHandlers;
    }

    public static List<SourcePostHandler> getPostHandlers(){
        return postHandlers;
    }

    public static boolean hasPreHandlers(){
        return preHandlers.size() > 0;
    }

    public static boolean hasPostHandlers(){
        return postHandlers.size() > 0;
    }

    public static void clear(){
        preHandlers.clear();
        postHandlers.clear();
    }

    public static void main(String[] args) throws Exception {
        registSourcePreHandler(new SourcePreHandler(){
            @Override
            public String handler(String source) {
                return source.replaceAll("a", "");
            }
        });

        registSourcePostHandler(new SourcePostHandler(){
            @Override
            public String handler(String source) {
                return source + "a";
            }
        });

        String source = "load(\"a.js\")";
        for (SourcePreHandler preHandler: getPreHandlers()){
            source = preHandler.handler(source);
        }
        System.out.println(source);

        for (SourcePostHandler postHandler: getPostHandlers()){
            source = postHandler.handler(source);
        }
        System.out.println(source);
    }
}
